package com.xudow.app.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Date utilities
 *
 * @author lukecheng
 */
public class TimeUtils {

    public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String date2str(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * Format java.util.Date to string that represent user's birthday
     *
     * @param date Date to be formatted
     * @return
     */
    public static String birthday(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(date);
        }
    }

    /**
     * 获取这个月有几周
     */
    public static int getMonthWeeks(Date dateInWeek) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(dateInWeek);
        ca.setFirstDayOfWeek(Calendar.MONDAY);
        return ca.getActualMaximum(Calendar.WEEK_OF_MONTH);
    }

    /**
     * 获取 dateInWeek 时间所在周的所有日期
     *
     * @param dateInWeek
     * @return
     */
    public static String[] getDaysOfWeek(Date dateInWeek) {
        String[] days = new String[7];

        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");

        Calendar c = Calendar.getInstance();
        c.setTime(dateInWeek);

        while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            c.add(Calendar.DATE, -1);
        }

        for (int i = 0; i < 7; i++) {
            days[i] = sdf.format(c.getTime());
            c.add(Calendar.DATE, 1);
        }

        return days;
    }

    /**
     * 返回 @dateInWeek 日期所在的周(当月的第几周)
     *
     * @param dateInWeek
     * @return
     */
    public static int getMonthNum(Date dateInWeek) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateInWeek);

        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 返回 @dateInWeek 日期所在的周(当月的第几周)
     *
     * @param dateInWeek
     * @return
     */
    public static int getWeekNum(Date dateInWeek) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateInWeek);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);

        return calendar.get(Calendar.WEEK_OF_MONTH);
    }

    /**
     * 获取 @dateInWeek 日期所在的年
     *
     * @param dateInWeek
     * @return
     */
    public static int getYear(Date dateInWeek) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateInWeek);

        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取@dateInWeek所在日期是星期几
     *
     * @param dateInWeek
     * @return
     */
    public static int dayForWeek(String dateInWeek) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();

        try {
            c.setTime(format.parse(dateInWeek));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        int dayForWeek = 0;

        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }

        return dayForWeek;
    }
}
